<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.mongodb.client.MongoCursor,org.bson.Document,org.bson.types.ObjectId,java.util.Date" %>
<%@ page import="mongo.Department,mongo.Major,mongo.Class" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>学生信息设置</title>
</head>
<body>
	<%
		//学生信息
		Document result = (Document)session.getAttribute("result"); 
	   	ObjectId _id = result.getObjectId("_id");
	  	String name = result.getString("name");
	  	String username = result.getString("username");
	  	String type = result.getString("type");
	  	String password = result.getString("password");
	  	String sex = result.getString("sex");
	  	if(sex==null) {
		  	sex="男";
	  	}
	  	String birthday = result.getString("birthday");
	  	String phone = result.getString("phone");
	  	String email = result.getString("email");
	  	String department = result.getString("department");
	  	ObjectId departmentid = result.getObjectId("departmentid");
	  	String major = result.getString("major");
		ObjectId majorid = result.getObjectId("majorid");
	  	String grade = result.getString("grade");
	  	String class1 = result.getString("class");
	  	ObjectId classid = result.getObjectId("classid");
	  	
	  	//系专业班级
	  	Department Department = new Department();
		Major Major = new Major();
		Class Class = new Class();
	  	MongoCursor<Document> departmentList = Department.list();
    	MongoCursor<Document> majorList = Major.list();
    	MongoCursor<Document> classList = Class.list();
	%>
	<jsp:include page="navbar.jsp" />
	<div class="container-fluid">
		<div class="row mt20">
			<div class="col-md-2 col-md-offset-2">
				<div class="panel b">
					<div class="panel-heading bg-gray-lighter text-bold">个人资料</div>
	            	<div class="list-group">
	            		<a href="#tabSetting1" data-toggle="tab" class="list-group-item"><em class="fa fa-file mr-10"></em>基本信息</a>
	            		<a href="#tabSetting2" data-toggle="tab" class="list-group-item"><em class="fa fa-file mr-10"></em>其它信息</a>
	            		<a href="#tabSetting3" data-toggle="tab" class="list-group-item"><em class="fa fa-lock mr-10"></em>修改密码</a>                       	
	            	</div>
	        	</div>
	      	</div>
	      	<div class="col-md-6">
	        	<div class="tab-content p0 b0">
	            	<div id="tabSetting1" class="tab-pane active">
	               		<div class="panel">
                			<div class="panel-body">
                  				<h4 class="page-header mt0">基本信息</h4>
                  				<form name="basicinfoform" role="form" action="../common/profileServlet" method="post"  onsubmit="return checkbasic(this);" class="form-horizontal mt20">
	                  	    		<fieldset>
						            	<div class="form-group">
						            		<label class="col-md-3 control-label">姓名</label>
					                  		<div class="col-md-7">
					                     		<input class="form-control" type="text" name="name" value="<%=name==null?"":name %>" placeholder="请输入学生姓名" />
					                  		</div>
						               	</div> 
					                </fieldset>
		               				<fieldset>
					               		<div class="form-group">
					                  		<label class="col-md-3 control-label">学号</label>
				                  			<div class="col-md-7">
				                     			<input class="form-control" type="text" name="username" value="<%=username %>" placeholder="请输入学号" readonly />
				                  			</div>
				               			</div>	
				               		</fieldset> 
				                	<fieldset>
					               		<div class="form-group">
					               			<label class="col-md-3 control-label">性别</label>
					               			<div class="col-md-7">
					               				<label class="radio-inline c-radio">
		                                 			<input id="inlineradio1" type="radio" name="sex" value="男" <%=sex.equals("男") ? "checked" : ""%>>
		                                 			<span class="fa fa-circle"></span>男</label>
		                        			 	<label class="radio-inline c-radio">
		                           					<input id="inlineradio2" type="radio" name="sex" value="女" <%=sex.equals("女") ? "checked" : ""%>>
		                           					<span class="fa fa-circle"></span>女</label>
					               			</div>
					               		</div>	
				               		</fieldset>  
				               		<fieldset>
				               			 <div class="form-group">
					                  		<label class="col-md-3 control-label">生日</label>
				                  			<div class="col-md-7">
				                     			<input class="form-control" type="date" name="birthday" value="<%=birthday==null?"":birthday %>"placeholder="请输入学生生日" />
				                  			</div>
				               			</div>
				               		</fieldset> 
				               		<fieldset>
					              		<div class="form-group">
					                  		<label class="col-md-3 control-label">手机</label>
				                  			<div class="col-md-7">
				                     			<input class="form-control" type="text" name="phone" value="<%=phone==null?"":phone %>" placeholder="请输入手机号" />
				                  			</div>
				               			</div>
				               		</fieldset>
				               		<fieldset>
					              		<div class="form-group">
					                  		<label class="col-md-3 control-label">邮箱</label>
				                  			<div class="col-md-7">
				                     			<input class="form-control" type="text" name="email" value="<%=email==null?"":email %>" placeholder="请输入邮箱" />
				                  			</div>
				               			</div>
				               		</fieldset>  
				               		<input type="hidden" name="_id" value="<%=_id %>" />
				               		<input type="hidden" name="type" value="<%=type %>" />
				               		<div class="col-md-10 col-md-offset-1">
				               			<div class="col-md-6">
				               				<button type="submit" class="btn btn-primary btn-block" name="method" value="update">确定</button> 
				               			</div>
				               			<div class="col-md-6">
				               				<button type="reset" class="btn btn-default btn-block">重置</button> 
				               			</div>
				               		</div>				               		
			              		</form>
		               		</div>
		            	</div>
		            </div>
	            	<div id="tabSetting2" class="tab-pane">
	               		<div class="panel">
                			<div class="panel-body">
                  				<h4 class="page-header mt0">其它信息</h4>
                				<form name="otherinfoform" role="form" action="../common/profileServlet" method="post"  onsubmit="return checkOtherInfo(this);" class="form-horizontal mt20">
	                  	    		<fieldset>
					               		<div class="form-group">
					                  		<label class="col-md-3 control-label">系别</label>
				                  			<div class="col-md-7">
				                  				<select class="form-control" name="departmentid" id="selectDepart">		
				                  					<%if(departmentid==null||departmentid.equals("")){ %>		  
				                  						<option selected>请选择系</option>
				                  						<%while(departmentList.hasNext()) {
								                  			Document document = departmentList.next();%>					                  			
								                     		<option value="<%=document.getObjectId("_id") %>"><%=document.getString("name") %></option>
								                     	<%} 
									                } else {
									                	while(departmentList.hasNext()) {
								                  			Document document = departmentList.next();%>					                  			
								                     		<option value="<%=document.getObjectId("_id") %>"  <%=departmentid.equals(document.getObjectId("_id")) ? "selected" : ""%>><%=document.getString("name") %></option>
									                	<%}							                  		
								                    } %> 
								                 </select>
				                  			</div>
				               			</div>	
				               		</fieldset> 
				                	<fieldset>
					               		<div class="form-group">
					                  		<label class="col-md-3 control-label">专业</label>
				                  			<div class="col-md-7">
				                  				<select class="form-control" name="majorid" id="selectMajor" placeholder="请先选择系部">
									               <option selected>请选择专业</option>
									               <%	
								                  		while(majorList.hasNext()) {
								                  			Document docu = majorList.next();
								                  			if(docu.getObjectId("departmentid").equals(departmentid)) {
								                  				Document document = docu;%>
								                  				<option value="<%=document.getObjectId("_id") %>"  <%=majorid.equals(document.getObjectId("_id")) ? "selected" : ""%>><%=document.getString("name") %></option>
								                  		<%	}			                  		
								              
								                    } %> 
								                 </select>
				                  			</div>
				               			</div>
				               		</fieldset>  
				               		<fieldset>
				               			 <div class="form-group">
					                  		<label class="col-md-3 control-label">年级</label>
				                  			<div class="col-md-7">                			
				                     			<input class="form-control" type="text" name="grade" value="<%=grade==null?"":grade%>" placeholder="请输入年级" />
				                  			</div>
				               			</div>
				               		</fieldset> 
				               		<fieldset>
					              		<div class="form-group">
					                  		<label class="col-md-3 control-label">班级</label>
				                  			<div class="col-md-7">
				                  				<select class="form-control" name="classid" id="selectClass"  placeholder="请先选择系部和专业">
									                <option selected>请选择班级</option>
									                 <%	
									                	 while(classList.hasNext()) {
										                 	Document docu = classList.next();
								                  			if(docu.getObjectId("majorid").equals(majorid)) {
								                  				Document document = docu;%>
								                  				<option value="<%=document.getObjectId("_id") %>"  <%=classid.equals(document.getObjectId("_id")) ? "selected" : ""%>><%=document.getString("name") %></option>
								                  		<%	}	
								                    } %> 
								                 </select>
				                  			</div>
				               			</div>
				               		</fieldset>
				               		<input type="hidden" name="department" id="department">
				               		<input type="hidden" name="major" id="major">
				               		<input type="hidden" name="class" id="class">
				               		<input type="hidden" name="_id" value="<%=_id %>" />
				               		<input type="hidden" name="type" value="<%=type %>" />
				               		<div class="col-md-10 col-md-offset-1">
				               			<div class="col-md-6">
				               				<button type="submit" class="btn btn-primary btn-block" name="method" value="update">确定</button> 
				               			</div>
				               			<div class="col-md-6">
				               				<button type="reset" class="btn btn-default btn-block">重置</button> 
				               			</div>
				               		</div>				               		
		              			</form>
		               		</div>
		            	</div>
	              	</div>
	            	<div id="tabSetting3" class="tab-pane">
	               		<div class="panel">
                			<div class="panel-body">
                  				<h4 class="page-header mt0">修改密码</h4>
                  				<form name="passwordform" role="form" action="../common/profileServlet" method="post"  onsubmit="return checkpassword(this);" class="form-horizontal mt20">
	                  	    		<fieldset>
						            	<div class="form-group mt10">
						            		<label class="col-md-3 control-label">当前密码</label>
					                  		<div class="col-md-7">
					                     		<input class="form-control" type="password" name="nowPwd"  placeholder="请输入原始密码" />
					                  		</div>
						               	</div> 
					                </fieldset>
					                <fieldset>
						            	<div class="form-group mt10">
						            		<label class="col-md-3 control-label">新密码</label>
					                  		<div class="col-md-7">
					                     		<input class="form-control" type="password" name="newPwd" placeholder="新密码，密码长度6位以上" />
					                  		</div>
						               	</div> 
					                </fieldset>
					                <fieldset>
						            	<div class="form-group mt10">
						            		<label class="col-md-3 control-label">确认密码</label>
					                  		<div class="col-md-7">
					                     		<input class="form-control" type="password" name="newPwd2"  placeholder="请再一次输入密码" />
					                  		</div>
						               	</div> 
					                </fieldset>
					                <input type="hidden" name="_id" value="<%=_id %>">
					                <input type="hidden" name="password" value="<%=password %>">
					                <div class="col-md-10 col-md-offset-1">
				               			<div class="col-md-6">
				               				<button type="submit" class="btn btn-primary btn-block" name="method" value="changePwd">确定</button> 
				               			</div>
				               			<div class="col-md-6">
				               				<button type="reset" class="btn btn-default btn-block">重置</button> 
				               			</div>
				               		</div>	
					           </form>
                  			</div>
                  		</div>
                  	</div>
	            </div>
	      	</div>
	   </div>
	</div>
	<script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
	<script type="text/javascript">  
	    //表单提交前进行数据验证，数据合法则返回true表单提交
	    function checkbasic(basicinfoform) {   
	    	var re = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	    	if(basicinfoform.name.value=="" || basicinfoform.name.value==null) {  
		        swal("姓名不能为空","请重新输入","warning");  
		        return false;  
		    } 
		    else if(basicinfoform.birthday.value=="" || basicinfoform.birthday.value==null) {  
		    	swal("生日不能为空","请重新输入","warning");   
		        return false;  
		    }   
		    else if(basicinfoform.phone.value=="" || basicinfoform.phone.value==null) {  
		   	    swal("手机不能为空","请重新输入","warning");   
		        return false;  
		    }
		    else if(basicinfoform.email.value=="" || basicinfoform.email.value==null) {  
		   	    swal("邮箱不能为空","请重新输入","warning");   
		        return false;  
		    }
		    else if(!re.test(basicinfoform.email.value)) {  
		   	    swal("邮箱格式有误","请重新输入","warning");   
		        return false;  
		    }
		    else {
		    	swal("修改个人资料成功","成功","success");   
		    	return true;
		    }   
	    }
	    
	    $(document).ready(function(){
	    	//系部下拉选择框变化
	    	$("#selectDepart").change(function(){
	    		var checkValue=$("#selectDepart").val();  
	    		$.ajax({
	                 type: "post",
	                 url: "../common/profileServlet?method=getmajor&_id="+checkValue,
	                 dataType: "text",
	                 success: function(data){
	                	 var result = JSON.parse(data); 
	                	 $("#selectMajor").empty(); //empty()方法删除被选元素的子元素（option）
	                	 $("#selectClass").empty();                
	                	 $("#selectMajor").append("<option selected>请选择专业</option>");
	                	 $("#selectClass").append("<option selected>请选择班级</option>");
	                	 if(result.length<1) {
	                		 $("#selectMajor").append("<option>目前该系没有专业</option>");
	                	 }
	                	 for(var i=0; i<result.length;i++){              
	                         $("#selectMajor").append("<option value='"+result[i]._id+"'>"+result[i].name+"</option>");
	                	 }
	                	 //$('#selectMajor option:first').attr('selected','selected');
	                     console.log(result);//修改id为showMsg标签的html
	                 }, error: function(){
	                	 swal("请求出错","请重新选择","warning");
	                 }
	           })
	    	});
	    	
	    	//专业下拉选择框变化
	    	$("#selectMajor").change(function(){  
	    		var checkValue=$("#selectMajor").val();  
	    		$.ajax({
	                 type: "post",
	                 url: "../common/profileServlet?method=getclass&_id="+checkValue,
	                 dataType: "text",
	                 success: function(data){
	                	 var result = JSON.parse(data); 
	                	 $("#selectClass").empty(); //empty()方法删除被选元素的子元素（option）
	                	 if(result.length<1) {
	                		 $("#selectClass").append("<option>目前该专业没有班级</option>");
	                	 }
	                	 for(var i=0; i<result.length;i++){              
	                         $("#selectClass").append("<option value='"+result[i]._id+"'>"+result[i].name+"</option>");
	                	 }
	                     console.log("class:"+result);//修改id为showMsg标签的html
	                 }, error: function(){
	                	 swal("请求出错","请重新选择","warning");
	                 }
	           })
	    	});
	  	});
	    
	    function checkOtherInfo(otherinfoform) {
	    	otherinfoform.department.value =$("#selectDepart").find("option:selected").text(); 
	    	otherinfoform.major.value =$("#selectMajor").find("option:selected").text();  
	    	otherinfoform.class.value =$("#selectClass").find("option:selected").text();  
	    	if(otherinfoform.department.value=="请选择系") {
	 			swal("系部不能为空","请重新选择","warning");
	 			return false;  
	 		}
	 		else if(otherinfoform.major.value=="请选择专业" || otherinfoform.major.value=="目前该系没有专业") {
	 			swal("专业不能为空","请重新选择","warning");
	 			return false;  
	 		}
	 		else if(otherinfoform.grade.value=="") {  
		        swal("年级不能为空","请重新输入","warning");  
		        return false;  
		    } 
	 		else if(otherinfoform.class.value=="请选择班级" || otherinfoform.class.value=="目前该专业没有班级") {
	 			swal("班级不能为空","请重新选择","warning");
	 			return false;  
	 		}
	 		else {
		    	swal("修改个人资料成功","成功","success");   
		    	return true;
		    }   
	    }
	    
	    function checkpassword(passwordform) {   
	    	if(passwordform.nowPwd.value=="") {  
		        swal("当前密码不能为空","请重新输入","warning");  
		        return false;  
		    } 
		    else if(passwordform.newPwd.value==""||passwordform.newPwd.value.length<6) {  
		   	    swal("新密码长度不小于6","请重新输入","warning");   
		        return false;  
		    }
		    else if(passwordform.newPwd.value!=passwordform.newPwd2.value) {  
		   	    swal("两次密码输入不相同","请重新输入","warning");   
		        return false;  
		    }
		    else if(passwordform.nowPwd.value!=passwordform.password.value) {  
		   	    swal("当前密码输入不正确","请重新输入","warning");   
		        return false;  
		    }
		    else {
		    	swal("修改密码成功","成功","success");   
		    	return true;
		    }      
	    }
	</script> 
</body>
</html>